import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;

public class Roster1 
{
	 public static void main(String[] args){
     database.openBusDatabase();
     PrintWriter output = null;
     PrintWriter output1 = null;
     try{
	 output = new PrintWriter(new FileWriter("driverTimeTable.txt"));
         output1 = new PrintWriter(new FileWriter("timeTable.txt"));
	 //get out the drivers
	 int[] driverIDs = DriverInfo.getDrivers();
	 Driver[] drivers = new Driver[driverIDs.length];
	 for(int i=0;i<driverIDs.length;i++)
	 {
	    drivers[i] = new Driver(driverIDs[i], 
            DriverInfo.getName(driverIDs[i]), DriverInfo.getHoursThisWeek(driverIDs[i]));
	 }//for

	//System.out.println("Name "+ drivers[i].name +  " hoursThisWeek:" + drivers[i].hoursThisWeek) ;}
	//System.out.println("No of services: " + TimetableInfo.getNumberOfServices(66));

	//for(int i =0;i<serviceTimes.length;i++)
	//System.out.println("Time " + i + ": " + serviceTimes[i]);

	//generate Roster for one Week
	Date theDate = new Date(113,5,17);

	for(int count=1;count<=7;count++)
	{
            allocateHoliday(theDate,drivers);
            //Get the IDs of all the routes within the scope of the pilot IBMS
            int[] busRoutes= BusStopInfo.getRoutes();
            for(int i =0;i<busRoutes.length;i++)
            {  //System.out.println("Route " + i + ": " + busRoutes[i]);
               output1.println("Route " + i + ": " + busRoutes[i]);
               //Get all the services in a route for the given timetable kind.	
               //getServices(int route, TimetableInfo.timetableKind kind) 
               int[] services = TimetableInfo.getServices(busRoutes[i],TimetableInfo.timetableKind(theDate));
               for(int j=0;j<services.length;j++)
               {//  System.out.print("[Service no: "+ services[j]); 
                  output1.print("[Service no: "+ services[j]);
        	  // Get the service times on a given route today for a particular service.
        	  int[] serviceTimes=TimetableInfo.getServiceTimes(busRoutes[i],theDate,services[j]-services[0]);
        	  int start=serviceTimes[0],end=serviceTimes[serviceTimes.length-1],theService= services[j];

        	  // Arrays.sort(drivers);
        	  Driver.assignNextBestDriver(start, end,drivers,theService,theDate);

        	  //  System.out.print(" During time: "+(serviceTimes[serviceTimes.length-1]-serviceTimes[0]));
        	  //  System.out.print("Best driver: "+ bestFit.name + "]: ");
        	  output1.print(" Service Time: ");
                  for(int k=0;k<serviceTimes.length;k++){
		  //	System.out.print(" " + serviceTimes[k]);
                      output1.print(" " + serviceTimes[k]);
                  }

        	  for(int m=0;m<drivers.length;m++){
        	    for(int n=0;n<10;n++){
        	      if(drivers[m].service[n]==theService){
                         //System.out.println(" driver allocated: "+ drivers[m].name);
                         output1.println(" driver allocated: "+ drivers[m].name);
                      }//if
                    }//for
                  }//for driver
        	  output1.println("");
                  //System.out.println("");

               }//for
            }//for routes
            //update Driver dayTimeTable; 
            for(int i =0;i<drivers.length;i++){ 
		drivers[i].updateDriverTimeTable(theDate);
		Driver.sortSmallArray(drivers[i].startMin,drivers[i].endMin,drivers[i].service);
	    }

            //go to next day
            theDate.setDate(theDate.getDate()+1);

	}//for each day of the week
	for(int i =0;i<drivers.length;i++)
	{  
	   if(drivers[i].hoursThisWeek!=0)
	   {  
	    System.out.println("------------------------------------------------------------------------------------");
            output.println("------------------------------------------------------------------------------------");
	    System.out.println(drivers[i].oneWeekTimetable);
            output.println(drivers[i].oneWeekTimetable);
	   } 
	}
     }//try
     catch (Exception exception)
     {
        System.err.println("can not output to file");
     }//catch
     finally
     {
       if (output != null)
       {
          output.close();
	  if (output.checkError())
	      System.err.println ("Something went wrong with the output");
       }// if
       if (output1 != null)
       {
          output1.close();
	  if (output1.checkError())
	      System.err.println ("Something went wrong with the output");
       }// if
     }// finally
   }//main

   //allocate holidays for the given day
   public static void allocateHoliday(Date theDate, Driver[] drivers)
   {
	int i=0;
	int howManyLeft= drivers.length/7;
	while(howManyLeft>0&&i<drivers.length)
	{
	   if(drivers[i].holidays2<2)
           {
	      drivers[i].holidays2++;
	      DriverInfo.setAvailable(drivers[i].number,theDate,false);
	      howManyLeft--;
	   }
           i++;
	}//while
   }//allocateHoliday
}